using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Models;
using System.Text.RegularExpressions;

namespace FiltronASP.Helpers.CatalogueHelpers
{
    public class FiltronHelper
    {
        public static String GetFiltronFilterWixNumber(String filterWixNumber, String filterWixNumberHd, bool isHdTruck)
        {
            if (!isHdTruck)
                return GetFiltronFilterWixNumber(filterWixNumber, filterWixNumberHd);
            else 
            {
                try
                {
                    if (!String.IsNullOrEmpty(filterWixNumber))
                    {
                        filterWixNumber = Regex.Replace(filterWixNumber, @"\s", "");
                    }   //if                
                    if (!String.IsNullOrEmpty(filterWixNumberHd))
                    {
                        filterWixNumberHd = Regex.Replace(filterWixNumberHd, @"\s", "");
                    }   //if                                
                    if ((!(String.IsNullOrEmpty(filterWixNumber))) && (!(String.IsNullOrEmpty(filterWixNumberHd))))
                    {
                        int result;
                        if (filterWixNumber.ToUpper().StartsWith("W"))
                        {
                            if (!String.IsNullOrEmpty(filterWixNumberHd))
                            {
                                if (Int32.TryParse("" + filterWixNumberHd[0], out result))
                                {
                                    return filterWixNumberHd;
                                }   //if
                            }
                        }   //if
                        if (filterWixNumberHd.ToUpper().StartsWith("W") && (Int32.TryParse("" + filterWixNumber[0], out result)))
                        {
                            if (!String.IsNullOrEmpty(filterWixNumber))
                            {
                                if (Int32.TryParse("" + filterWixNumber[0], out result))
                                {
                                    return filterWixNumber;
                                }   //if
                            }
                        }   //if
                        if (filterWixNumber.ToUpper().StartsWith("W"))
                        {
                            return filterWixNumber;
                        }   //if
                        if (filterWixNumberHd.ToUpper().StartsWith("W"))
                        {
                            return filterWixNumberHd;
                        }   //if
                        return filterWixNumber;
                    }   //if
                    else if (!(String.IsNullOrEmpty(filterWixNumber)))
                    {
                        return filterWixNumber;
                    }   //if
                    else if (!(String.IsNullOrEmpty(filterWixNumberHd)))
                    {
                        return filterWixNumberHd;
                    }   //if
                    return String.Empty;
                }   //try
                catch (Exception ex)
                {
                    Logger.MyLogger.Log(ex);
                }   //catch
                return String.Empty;
            }
        }
        /// <summary>
        /// Metoda do pobierania numeru WIX dla filtrow Filtrona.
        /// </summary>
        /// <param name="filterWixNumber">numer wix</param>
        /// <param name="param name="filterWixNumberHd">numer wix hd</param>
        /// <returns></returns>
        public static String GetFiltronFilterWixNumber(String filterWixNumber, String filterWixNumberHd)
        {
            try
            {
                if (!String.IsNullOrEmpty(filterWixNumber))
                {
                    if (filterWixNumber.ToLower() != "in preparation")
                    {
                        filterWixNumber = Regex.Replace(filterWixNumber, @"\s", "");
                    }   //if
                }   //if                
                if (!String.IsNullOrEmpty(filterWixNumberHd))
                {
                    if (filterWixNumberHd.ToLower() != "in preparation")
                    {
                        filterWixNumberHd = Regex.Replace(filterWixNumberHd, @"\s", "");
                    }   //if
                }   //if                                
                if ((!(String.IsNullOrEmpty(filterWixNumber))) && (!(String.IsNullOrEmpty(filterWixNumberHd))))
                {
                    if (filterWixNumber.ToUpper().StartsWith("W"))
                    {
                        return filterWixNumber;
                    }   //if
                    if (filterWixNumberHd.ToUpper().StartsWith("W"))
                    {
                        return filterWixNumberHd;
                    }   //if
                    if (filterWixNumber == "N.I.")
                    {
                        return "X";
                    }   //if
                    if (filterWixNumber == "N.W.")
                    {
                        return "∞";
                    }   //if
                    return filterWixNumber;
                }   //if
                else if (!(String.IsNullOrEmpty(filterWixNumber)))
                {
                    return filterWixNumber;
                }   //if
                else if (!(String.IsNullOrEmpty(filterWixNumberHd)))
                {
                    return filterWixNumberHd;
                }   //if
                return String.Empty;
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
            }   //catch
            return String.Empty;
        }

        public static string GetFiltronFilterNumber(FiltronFilter filter, bool isHdTruck)
        {
            if (filter == null)
            {
                return String.Empty;
            }   //if
            if (!isHdTruck)
            {
                return GetFiltronFilterNumber(filter);
            }
            else
            {
                String filterNumber = FiltronHelper.GetFiltronFilterWixNumber(filter.filter_wix_number, filter.filter_wix_number_hd, isHdTruck);
                if (!(String.IsNullOrEmpty(filterNumber)))
                {
                    return filterNumber;
                }   //if
                return Regex.Replace(filter.filter_number, @"\s", "");
            }
        }

        /// <summary>
        /// Metoda pobierania numeru filtra.
        /// </summary>
        /// <param name="filter">filtr</param>
        /// <returns>numer z wixa</returns>
        public static string GetFiltronFilterNumber(FiltronFilter filter)
        {
            if (filter == null)
            {
                return String.Empty;
            }   //if
            String filterNumber = FiltronHelper.GetFiltronFilterWixNumber(filter.filter_wix_number, filter.filter_wix_number_hd);
            if (!(String.IsNullOrEmpty(filterNumber)))
            {
                return filterNumber;
            }   //if
            if (!String.IsNullOrEmpty(filter.filter_number))
            {
                return Regex.Replace(filter.filter_number, @"\s", "");
            }   //if
            return String.Empty;
        }

        public static string GetFiltronFilterImage(string filterMedia) {
            if (filterMedia.Length.Equals(0)) {
                return String.Empty;
            }
            char firstChar = filterMedia[0];
            char firstCharConverted = ' ';
            switch (firstChar) {
                case '1':
                    firstCharConverted = 'A';
                    break;
                case '2':
                    firstCharConverted = 'L';
                    break;
                case '3':
                    firstCharConverted = 'F';
                    break;
                case '4':
                    firstCharConverted = 'K';
                    break;
            }
            return Convert.ToString(firstCharConverted) + filterMedia[1] + filterMedia[2];
        }



    }
}
